package com.snow.useviewdemo.function.wallpaper;

import android.os.Build;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;

public class SZWallpaperService extends WallpaperService {

    private WallpaperEngine mWallpaperEngine = null;

    @Override
    public Engine onCreateEngine() {
        mWallpaperEngine = new WallpaperEngine();
        return mWallpaperEngine;
    }

    public class WallpaperEngine extends Engine {
        private SurfaceHolder mSurfaceHolder = null;
        private SZWallpaperView mSZWallpaperView = null;

        public WallpaperEngine() {
            this.mSurfaceHolder = getSurfaceHolder();
            mSZWallpaperView = new SZWallpaperView(this, SZWallpaperService.this.getBaseContext());
            mSZWallpaperView.initView();
            drawView();
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { //ICE_CREAM_SANDWICH_MR1  15
                return;
            }
            setOffsetNotificationsEnabled(true);
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            this.drawView();
            if (this.mSZWallpaperView != null) {
                this.mSZWallpaperView.surfaceCreated(holder);
            }
        }


        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
            this.drawView();
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            if (this.mSZWallpaperView != null) {
                this.mSZWallpaperView.surfaceDestroyed(holder);
            }
        }

        /**
         * 绘制View
         */
        private void drawView() {
            if (mSZWallpaperView == null) {
                return;
            }
            mSZWallpaperView.surfaceChanged(mSurfaceHolder, -1, mSZWallpaperView.getWidth(), mSZWallpaperView.getHeight());
            if (!isVisible()) {
                return;
            }
            mSZWallpaperView.loadDefaultWallpaperBitmap();
        }
    }
}
